package linkedlist.exercise;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 循环队列
 * <p> 借助 size 实现
 * @author yifeng
 */
public class CircularQueue {
    int[] table;
    int size = 0;
    int head = 0;
    int tail = 0;
    public CircularQueue(int capacity) {
        this.table = new int[capacity];
    }

    /**
     * 入队
     * @param item 元素
     * @return
     */
    public boolean enqueue(int item) {
        if(size == table.length) {
            return false;
        }
        if(tail == table.length) {
            tail = 0;
        }
        table[tail++] = item;
        size++;
        return true;
    }

    /**
     * 出队
     * @return
     */
    public int dequeue() {
        if(size == 0) {
            return -1;
        }
        if(head == table.length) {
            head = 0;
        }
        size--;
        return table[head++];
    }

    public static void main(String[] args) {
        CircularQueue queue = new CircularQueue(2);
        System.out.println(queue.dequeue());
        System.out.println(queue.enqueue(0));
        System.out.println(queue.enqueue(2));
        System.out.println(queue.enqueue(3));
        System.out.println(queue.dequeue());
        System.out.println(queue.enqueue(5));
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        List<Integer> list = new ArrayList<>();
        String i = 1 + 1 + 1 + "";
        System.out.println(i);
    }
}
